<?php namespace App\Http\Services;

use App\Userinfo;
use App\WxAuth;
use Auth;
use Blocktrail\CryptoJSAES\CryptoJSAES;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Cookie;
use Illuminate\Support\Facades\Hash;
use App\User;

class UserManager
{
    public static function isWxOpenid(Request $request = null){
        if (!isset($_COOKIE['user_token'])) {
            if (is_null($request)) {
                return false;
            } else {
                $token = !is_null($request->header('user_token')) ? $request->header('user_token') : $request->get('user_token');
                if (!is_null($token)) {
                    $openid = false;
                    try {
                        $openid = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
                    } catch (\Exception $e) {
                        return false;
                    }

                    if (!$openid)
                        return false;

                    if(strpos($openid,'wx_openid@') !== false){
                        $openid = str_replace("wx_openid@","",$openid);
                        $thatWxAuth = WxAuth::whereWxOpenid($openid)->first();
                        if(!$thatWxAuth){
                            return false;
                        }

                        return $thatWxAuth;
                    }else{
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } else {
            $token = Cookie::get('user_token');

            if (!$token || is_null($token))
                return false;

            $openid = false;
            try {
                $openid = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
            } catch (\Exception $e) {
                return false;
            }

            if (!$openid)
                return false;

            if(strpos($openid,'wx_openid@') !== false){
                $openid = str_replace("wx_openid@","",$openid);
                $thatWxAuth = WxAuth::whereWxOpenid($openid)->first();
                if(!$thatWxAuth){
                    return false;
                }

                return $thatWxAuth;
            }else{
                return false;
            }
        }
    }

    public static function isLogin(Request $request = null)
    {
        if (!isset($_COOKIE['user_token'])) {
            if (is_null($request)) {
                return false;
            } else {
                $token = !is_null($request->header('user_token')) ? $request->header('user_token') : $request->get('user_token');
                if (!is_null($token)) {

                    $openid = false;
                    try {
                        $openid = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
                    } catch (\Exception $e) {
                        return false;
                    }

                    if (!$openid)
                        return false;

                    $thatUser = User::whereRzKey($openid)->first();
                    if (!$thatUser)
                        return false;

                    return $thatUser;
                } else {
                    return false;
                }
            }
        } else {
            $token = Cookie::get('user_token');

            if (!$token || is_null($token))
                return false;

            $openid = false;
            try {
                $openid = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
            } catch (\Exception $e) {
                return false;
            }

            if (!$openid)
                return false;

            $thatUser = User::whereRzKey($openid)->first();

            if (!$thatUser)
                return false;

            return $thatUser;
        }
    }

    public static function buildRzKey()
    {
        $num = mt_rand(1000, 9999);
        $rzKey = md5(time() . $num);
        return $rzKey;
    }

    public static function signInByPhone($phone,$password)
    {
        $user = User::wherePhone($phone)->first();
        if (!Hash::check($password, $user->password))
            return false;

        return $user;


    }

    public static function isPhoneExists($phone)
    {
        $phone = User::wherePhone($phone)->first();
        if (!$phone)
            return false;
        return $phone;
    }

    /**
     * �û���¼��Ӧ����
     * @param $username
     * @param $email
     * @param $phone
     * @param $password
     * @return User
     */
    public static function signUp($phone, $password)
    {
        $user = new User();
        $user->phone = $phone;
        $user->password = bcrypt($password);
        $user->rz_key = self::buildRzKey();
        $user->status = 0;
        $user->save();

        return $user;
    }

}